<?php

namespace iBrand\Component\Purchase\Models;

use iBrand\Component\Goods\Models\GoodsProduct;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;

class PurchaseItem extends Model implements Transformable
{
	use SoftDeletes, TransformableTrait;

	protected $guarded = ['id'];

	public function __construct(array $attributes = [])
	{
		parent::__construct($attributes);

		$prefix = config('ibrand.app.database.prefix', 'ibrand_');

		$this->setTable($prefix . 'store_purchase_item');
	}

	public function purchase()
	{
		return $this->belongsTo(Purchase::class, 'purchase_id');
	}

	public function product()
	{
		return $this->belongsTo(GoodsProduct::class, 'item_id');
	}

	public function setMetaAttribute($value)
	{
		$this->attributes['meta'] = json_encode($value);
	}

	public function getMetaAttribute($value)
	{
		return json_decode($value, true);
	}

}